home *** CD-ROM | disk | FTP | other *** search
/ Apple Developer Connection Student Program / ADC Tools Sampler CD Disk 3 1999.iso / Metrowerks CodeWarrior / Java Support / Java_Source / Java2 / src / javax / swing / JLabel.java < prev    next >
Encoding:
Java Source  |  1999-05-28  |  30.7 KB  |  955 lines  |  [TEXT/CWIE]

  1. /*
  2.  * @(#)JLabel.java    1.84 98/08/28
  3.  *
  4.  * Copyright 1997, 1998 by Sun Microsystems, Inc.,
  5.  * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
  6.  * All rights reserved.
  7.  *
  8.  * This software is the confidential and proprietary information
  9.  * of Sun Microsystems, Inc. ("Confidential Information").  You
  10.  * shall not disclose such Confidential Information and shall use
  11.  * it only in accordance with the terms of the license agreement
  12.  * you entered into with Sun.
  13.  */
  14.  
  15. package javax.swing;
  16.  
  17. import java.awt.Component;
  18. import java.awt.Font;
  19. import java.awt.Image;
  20.  
  21. import java.io.ObjectOutputStream;
  22. import java.io.ObjectInputStream;
  23. import java.io.IOException;
  24.  
  25. import javax.swing.plaf.LabelUI;
  26. import javax.accessibility.*;
  27.  
  28.  
  29. /**
  30.  * A display area for a short text string or an image,
  31.  * or both.
  32.  * A label does not react to input events.
  33.  * As a result, it cannot get the keyboard focus.
  34.  * A label can, however, display a keyboard alternative
  35.  * as a convenience for a nearby component
  36.  * that has a keyboard alternative but can't display it.
  37.  * <p>
  38.  * A <code>JLabel</code> object can display
  39.  * either text, an image, or both.
  40.  * You can specify where in the label's display area
  41.  * the label's contents are aligned
  42.  * by setting the vertical and horizontal alignment.
  43.  * By default, labels are vertically centered 
  44.  * in their display area.
  45.  * Text-only labels are left-aligned, by default;
  46.  * image-only labels are horizontally centered, by default.
  47.  * <p>
  48.  * You can also specify the position of the text
  49.  * relative to the image.
  50.  * By default, text is to the right of the image,
  51.  * with the text and image vertically aligned.
  52.  * <p>
  53.  * Finally, you can use the <code>setIconTextGap</code> method
  54.  * to specify how many pixels
  55.  * should appear between the text and the image.
  56.  * The default is 4 pixels.
  57.  * <p>
  58.  * See <a href="http://java.sun.com/docs/books/tutorial/ui/swing/label.html">How to Use Labels</a>
  59.  * in <a href="http://java.sun.com/Series/Tutorial/index.html"><em>The Java Tutorial</em></a>
  60.  * for further documentation.
  61.  * <p>
  62.  * <strong>Warning:</strong>
  63.  * Serialized objects of this class will not be compatible with 
  64.  * future Swing releases.  The current serialization support is appropriate
  65.  * for short term storage or RMI between applications running the same
  66.  * version of Swing.  A future release of Swing will provide support for
  67.  * long term persistence.
  68.  *
  69.  * @beaninfo
  70.  *   attribute: isContainer false
  71.  * description: A component that displays a short string and an icon.
  72.  * 
  73.  * @version 1.84 08/28/98
  74.  * @author Hans Muller
  75.  */
  76. public class JLabel extends JComponent implements SwingConstants, Accessible
  77. {
  78.     /**
  79.      * @see #getUIClassID
  80.      * @see #readObject
  81.      */
  82.     private static final String uiClassID = "LabelUI";
  83.  
  84.     private int mnemonic = '\0';
  85.  
  86.     private String text = "";         // "" rather than null, for BeanBox
  87.     private Icon defaultIcon = null;
  88.     private Icon disabledIcon = null;
  89.     private boolean disabledIconSet = false;
  90.             
  91.     private int verticalAlignment = CENTER;
  92.     private int horizontalAlignment = LEFT;
  93.     private int verticalTextPosition = CENTER;
  94.     private int horizontalTextPosition = RIGHT;
  95.     private int iconTextGap = 4;
  96.  
  97.     protected Component labelFor = null;
  98.  
  99.  
  100.     /**
  101.      * Client property key used to determine what label is labeling the
  102.      * component.  This is generally not used by labels, but is instead
  103.      * used by components such as text areas that are being labeled by
  104.      * labels.  When the labelFor property of a label is set, it will
  105.      * automatically set the LABELED_BY_PROPERTY of the component being
  106.      * labelled.
  107.      *
  108.      * @see #setLabelFor
  109.      */
  110.     static final String LABELED_BY_PROPERTY = "labeledBy";
  111.  
  112.     /**
  113.      * Creates a <code>JLabel</code> instance with the specified
  114.      * text, image, and horizontal alignment.
  115.      * The label is centered vertically in its display area.
  116.      * The text is to the right of the image.
  117.      *
  118.      * @param text  The text to be displayed by the label.
  119.      * @param icon  The image to be displayed by the label.
  120.      * @param horizontalAlignment  One of the following constants
  121.      *           defined in <code>SwingConstants</code>:
  122.      *           <code>LEFT</code>,
  123.      *           <code>CENTER</code>, or
  124.      *           <code>RIGHT</code>.
  125.      */
  126.     public JLabel(String text, Icon icon, int horizontalAlignment) {
  127.         setText(text);
  128.         setIcon(icon);
  129.         setHorizontalAlignment(horizontalAlignment);
  130.         updateUI();
  131.         setAlignmentX(LEFT_ALIGNMENT);
  132.     }
  133.             
  134.     /**
  135.      * Creates a <code>JLabel</code> instance with the specified
  136.      * text and horizontal alignment.
  137.      * The label is centered vertically in its display area.
  138.      *
  139.      * @param text  The text to be displayed by the label.
  140.      * @param horizontalAlignment  One of the following constants
  141.      *           defined in <code>SwingConstants</code>:
  142.      *           <code>LEFT</code>,
  143.      *           <code>CENTER</code>, or
  144.      *           <code>RIGHT</code>.
  145.      */
  146.     public JLabel(String text, int horizontalAlignment) {
  147.         this(text, null, horizontalAlignment);
  148.     }
  149.  
  150.     /**
  151.      * Creates a <code>JLabel</code> instance with the specified text.
  152.      * The label is aligned against the left side of its display area,
  153.      * and centered vertically.
  154.      *
  155.      * @param text  The text to be displayed by the label.
  156.      */
  157.     public JLabel(String text) {
  158.         this(text, null, LEFT);
  159.     }
  160.  
  161.     /**
  162.      * Creates a <code>JLabel</code> instance with the specified
  163.      * image and horizontal alignment.
  164.      * The label is centered vertically in its display area.
  165.      *
  166.      * @param icon  The image to be displayed by the label.
  167.      * @param horizontalAlignment  One of the following constants
  168.      *           defined in <code>SwingConstants</code>:
  169.      *           <code>LEFT</code>,
  170.      *           <code>CENTER</code>, or
  171.      *           <code>RIGHT</code>.
  172.      */
  173.     public JLabel(Icon image, int horizontalAlignment) {
  174.         this(null, image, horizontalAlignment);
  175.     }
  176.  
  177.     /**
  178.      * Creates a <code>JLabel</code> instance with the specified image.
  179.      * The label is centered vertically and horizontally
  180.      * in its display area.
  181.      *
  182.      * @param icon  The image to be displayed by the label.
  183.      */
  184.     public JLabel(Icon image) {
  185.         this(null, image, CENTER);
  186.     }
  187.  
  188.     /**
  189.      * Creates a <code>JLabel</code> instance with 
  190.      * no image and with an empty string for the title.
  191.      * The label is centered vertically 
  192.      * in its display area.
  193.      * The label's contents, once set, will be displayed at the left 
  194.      * of the label's display area.
  195.      */
  196.     public JLabel() {
  197.         this("", null, LEFT);
  198.     }
  199.  
  200.  
  201.     /**
  202.      * Returns the L&F object that renders this component.
  203.      *
  204.      * @return LabelUI object
  205.      */
  206.     public LabelUI getUI() {
  207.         return (LabelUI)ui;
  208.     }
  209.  
  210.  
  211.     /**
  212.      * Sets the L&F object that renders this component.
  213.      *
  214.      * @param ui  the LabelUI L&F object
  215.      * @see UIDefaults#getUI
  216.      * @beaninfo
  217.      *      expert: true
  218.      *  description: The L&F object that renders this component.
  219.      */
  220.     public void setUI(LabelUI ui) {
  221.         super.setUI(ui);
  222.     }
  223.  
  224.  
  225.     /**
  226.      * Notification from the UIFactory that the L&F
  227.      * has changed. 
  228.      *
  229.      * @see JComponent#updateUI
  230.      */
  231.     public void updateUI() {
  232.         setUI((LabelUI)UIManager.getUI(this));
  233.     }
  234.  
  235.  
  236.     /**
  237.      * Returns a string that specifies the name of the l&f class
  238.      * that renders this component.
  239.      *
  240.      * @return String "LabelUI"
  241.      *
  242.      * @see JComponent#getUIClassID
  243.      * @see UIDefaults#getUI
  244.      */
  245.     public String getUIClassID() {
  246.         return uiClassID;
  247.     }
  248.  
  249.  
  250.     /** 
  251.      * Returns the text string that the label displays.
  252.      *
  253.      * @return a String
  254.      * @see #setText
  255.      */
  256.     public String getText() {
  257.         return text;
  258.     }
  259.  
  260.  
  261.     /**
  262.      * Defines the single line of text this component will display.  If
  263.      * the value of text is null or empty string, nothing is displayed.
  264.      * <p>
  265.      * The default value of this property is null.
  266.      * <p>
  267.      * This is a JavaBeans bound property.  
  268.      * 
  269.      * @see #setVerticalTextPosition
  270.      * @see #setHorizontalTextPosition
  271.      * @see #setIcon
  272.      * @beaninfo
  273.      *    preferred: true
  274.      *        bound: true
  275.      *    attribute: visualUpdate true
  276.      *  description: Defines the single line of text this component will display.
  277.      */
  278.     public void setText(String text) {
  279.  
  280.         String oldAccessibleName = null;
  281.         if (accessibleContext != null) {
  282.             oldAccessibleName = accessibleContext.getAccessibleName();
  283.         }
  284.  
  285.         String oldValue = this.text;
  286.         this.text = text;
  287.         firePropertyChange("text", oldValue, text);
  288.  
  289.         if ((accessibleContext != null) 
  290.             && (accessibleContext.getAccessibleName() != oldAccessibleName)) {
  291.                 accessibleContext.firePropertyChange(
  292.                         AccessibleContext.ACCESSIBLE_VISIBLE_DATA_PROPERTY, 
  293.                         oldAccessibleName,
  294.                         accessibleContext.getAccessibleName());
  295.         }
  296.         if (text == null || oldValue == null || !text.equals(oldValue)) {
  297.             revalidate();
  298.             repaint();
  299.         }
  300.     }
  301.  
  302.     
  303.     /**
  304.      * Returns the graphic image (glyph, icon) that the label displays.
  305.      *
  306.      * @return an Icon
  307.      * @see #setIcon
  308.      */
  309.     public Icon getIcon() {
  310.         return defaultIcon;
  311.     }
  312.  
  313.     /**
  314.      * Defines the icon this component will display.  If
  315.      * the value of icon is null, nothing is displayed.
  316.      * <p>
  317.      * The default value of this property is null.
  318.      * <p>
  319.      * This is a JavaBeans bound property.  
  320.      * 
  321.      * @see #setVerticalTextPosition
  322.      * @see #setHorizontalTextPosition
  323.      * @see #getIcon
  324.      * @beaninfo
  325.      *    preferred: true
  326.      *        bound: true
  327.      *    attribute: visualUpdate true
  328.      *  description: The icon this component will display.
  329.      */
  330.     public void setIcon(Icon icon) {
  331.         Icon oldValue = defaultIcon;
  332.         defaultIcon = icon;
  333.  
  334.         /* If the default icon has really changed and we had
  335.          * generated the disabled icon for this component,
  336.          * (i.e. setDiabledIcon() was never called) then 
  337.          * clear the disabledIcon field.
  338.          */
  339.         if ((defaultIcon != oldValue) && !disabledIconSet) {
  340.             disabledIcon = null;
  341.         }
  342.  
  343.         firePropertyChange("icon", oldValue, defaultIcon);
  344.  
  345.         if ((accessibleContext != null) && (oldValue != defaultIcon)) {
  346.                 accessibleContext.firePropertyChange(
  347.                         AccessibleContext.ACCESSIBLE_VISIBLE_DATA_PROPERTY, 
  348.                         oldValue, defaultIcon);
  349.         }
  350.  
  351.         /* If the default icon has changed and the new one is 
  352.          * a different size, then revalidate.   Repaint if the
  353.          * default icon has changed.
  354.          */
  355.         if (defaultIcon != oldValue) {
  356.             if ((defaultIcon == null) || 
  357.                 (oldValue == null) ||
  358.                 (defaultIcon.getIconWidth() != oldValue.getIconWidth()) ||
  359.                 (defaultIcon.getIconHeight() != oldValue.getIconHeight())) {
  360.                 revalidate();
  361.             } 
  362.             repaint();
  363.         }
  364.     }
  365.  
  366.  
  367.     /**
  368.      * Returns the value of the disabledIcon property if it's been set,
  369.      * If it hasn't been set and the value of the icon property is
  370.      * an ImageIcon, we compute a "grayed out" version of the icon and
  371.      * update the disabledIcon property with that.
  372.      * 
  373.      * @return The value of the disabledIcon property.
  374.      * @see #setDisabledIcon
  375.      * @see ImageIcon
  376.      */
  377.     public Icon getDisabledIcon() 
  378.     {
  379.         if(!disabledIconSet && 
  380.            (disabledIcon == null) &&
  381.            (defaultIcon != null) && 
  382.            (defaultIcon instanceof ImageIcon)) {
  383.             Image grayImage = GrayFilter.createDisabledImage(((ImageIcon)defaultIcon).getImage());
  384.             disabledIcon = new ImageIcon(grayImage);
  385.             firePropertyChange("disabledIcon", null, disabledIcon);
  386.         }
  387.         return disabledIcon;
  388.     }
  389.  
  390.  
  391.     /**
  392.      * Set the icon to be displayed if this JLabel is "disabled", i.e.
  393.      * JLabel.setEnabled(false).
  394.      * <p>
  395.      * The default value of this property is null.
  396.      * 
  397.      * @param disabledIcon the Icon to display when the component is disabled
  398.      * @see #getDisabledIcon
  399.      * @see #setEnabled
  400.      * @beaninfo
  401.      *        bound: true
  402.      *    attribute: visualUpdate true
  403.      *  description: The icon to display if the label is disabled.
  404.      */
  405.     public void setDisabledIcon(Icon disabledIcon) {
  406.         Icon oldValue = this.disabledIcon;
  407.         this.disabledIcon = disabledIcon;
  408.         disabledIconSet = true;
  409.         firePropertyChange("disabledIcon", oldValue, disabledIcon);
  410.         if (disabledIcon != oldValue) {
  411.             if (disabledIcon == null || oldValue == null ||
  412.                 disabledIcon.getIconWidth() != oldValue.getIconWidth() ||
  413.                 disabledIcon.getIconHeight() != oldValue.getIconHeight()) {
  414.                 revalidate();
  415.             } 
  416.             if (!isEnabled()) {
  417.                 repaint();
  418.             }
  419.         }
  420.     }
  421.  
  422.  
  423.     /**
  424.      * Specify a keycode that indicates a mnemonic key.
  425.      * This property is used when the label is part of a larger component.  
  426.      * If the labelFor property of the label is not null, the label will
  427.      * call the requestFocus method of the component specified by the
  428.      * labelFor property when the mnemonic is activated.
  429.      *
  430.      * @see #getLabelFor
  431.      * @see #setLabelFor
  432.      * @beaninfo
  433.      *        bound: true
  434.      *    attribute: visualUpdate true
  435.      *  description: The mnemonic keycode.
  436.      */
  437.     public void setDisplayedMnemonic(int key) {
  438.         int oldKey = mnemonic;
  439.         mnemonic = key;
  440.         firePropertyChange("displayedMnemonic", oldKey, mnemonic);
  441.         if (key != oldKey) {
  442.             revalidate();
  443.             repaint();
  444.         }
  445.     }
  446.  
  447.  
  448.     /**
  449.      * Specifies the displayedMnemonic as a char value.
  450.      *
  451.      * @param aChar  a char specifying the mnemonic to display
  452.      * @see #setDisplayedMnemonic(int)
  453.      */
  454.     public void setDisplayedMnemonic(char aChar) {
  455.         int vk = (int) aChar;
  456.         if(vk >= 'a' && vk <='z')
  457.             vk -= ('a' - 'A');
  458.         setDisplayedMnemonic(vk);
  459.     }
  460.  
  461.  
  462.     /**
  463.      * Return the keycode that indicates a mnemonic key.
  464.      * This property is used when the label is part of a larger component.
  465.      * If the labelFor property of the label is not null, the label will
  466.      * call the requestFocus method of the component specified by the
  467.      * labelFor property when the mnemonic is activated.
  468.      *
  469.      * @return int value for the mnemonic key
  470.      *
  471.      * @see #getLabelFor
  472.      * @see #setLabelFor
  473.      */
  474.     public int getDisplayedMnemonic() {
  475.         return mnemonic;
  476.     }
  477.  
  478.  
  479.     /**
  480.      * Verify that key is a legal value for the horizontalAlignment properties.
  481.      *
  482.      * @param key the property value to check
  483.      * @param message the IllegalArgumentException detail message 
  484.      * @exception IllegalArgumentException if key isn't LEFT, CENTER, RIGHT,
  485.      * LEADING or TRAILING.
  486.      * @see #setHorizontalTextPosition
  487.      * @see #setHorizontalAlignment
  488.      */
  489.     protected int checkHorizontalKey(int key, String message) {
  490.         if ((key == LEFT) ||
  491.             (key == CENTER) ||
  492.             (key == RIGHT) ||
  493.             (key == LEADING) ||
  494.             (key == TRAILING)) {
  495.             return key;
  496.         }
  497.         else {
  498.             throw new IllegalArgumentException(message);
  499.         }
  500.     }
  501.  
  502.  
  503.     /**
  504.      * Verify that key is a legal value for the 
  505.      * verticalAlignment or verticalTextPosition properties.
  506.      *
  507.      * @param key the property value to check
  508.      * @param message the IllegalArgumentException detail message 
  509.      * @exception IllegalArgumentException if key isn't TOP, CENTER, or BOTTOM.
  510.      * @see #setVerticalAlignment
  511.      * @see #setVerticalTextPosition
  512.      */
  513.     protected int checkVerticalKey(int key, String message) {
  514.         if ((key == TOP) || (key == CENTER) || (key == BOTTOM)) {
  515.             return key;
  516.         }
  517.         else {
  518.             throw new IllegalArgumentException(message);
  519.         }
  520.     }
  521.  
  522.  
  523.     /**
  524.      * Returns the amount of space between the text and the icon
  525.      * displayed in this label.
  526.      *
  527.      * @return an int equal to the number of pixels between the text
  528.      *         and the icon.
  529.      * @see #setIconTextGap
  530.      */
  531.     public int getIconTextGap() {
  532.         return iconTextGap;
  533.     }
  534.  
  535.  
  536.     /**
  537.      * If both the icon and text properties are set, this property
  538.      * defines the space between them.  
  539.      * <p>
  540.      * The default value of this property is 4 pixels.
  541.      * <p>
  542.      * This is a JavaBeans bound property.
  543.      * 
  544.      * @see #getIconTextGap
  545.      * @beaninfo
  546.      *        bound: true
  547.      *    attribute: visualUpdate true
  548.      *  description: If both the icon and text properties are set, this
  549.      *               property defines the space between them.
  550.      */
  551.     public void setIconTextGap(int iconTextGap) {
  552.         int oldValue = this.iconTextGap;
  553.         this.iconTextGap = iconTextGap;
  554.         firePropertyChange("iconTextGap", oldValue, iconTextGap);
  555.         if (iconTextGap != oldValue) {
  556.             revalidate();
  557.             repaint();
  558.         }
  559.     }
  560.  
  561.  
  562.  
  563.     /**
  564.      * Returns the alignment of the label's contents along the Y axis.
  565.      *
  566.      * @return   The value of the verticalAlignment property, one of the 
  567.      *           following constants defined in <code>SwingConstants</code>:
  568.      *           <code>TOP</code>,
  569.      *           <code>CENTER</code>, or
  570.      *           <code>BOTTOM</code>.
  571.      *
  572.      * @see SwingConstants
  573.      * @see #setVerticalAlignment
  574.      */
  575.     public int getVerticalAlignment() {
  576.         return verticalAlignment;
  577.     }
  578.  
  579.  
  580.     /**
  581.      * Sets the alignment of the label's contents along the Y axis.  
  582.      * <p>
  583.      * The default value of this property is CENTER.
  584.      * 
  585.      * @param alignment One of the following constants
  586.      *           defined in <code>SwingConstants</code>:
  587.      *           <code>TOP</code>,
  588.      *           <code>CENTER</code> (the default), or
  589.      *           <code>BOTTOM</code>.
  590.      *
  591.      * @see SwingConstants
  592.      * @see #getVerticalAlignment
  593.      * @beaninfo
  594.      *        bound: true
  595.      *         enum: TOP    SwingConstants.TOP
  596.      *               CENTER SwingConstants.CENTER
  597.      *               BOTTOM SwingConstants.BOTTOM
  598.      *    attribute: visualUpdate true
  599.      *  description: The alignment of the label's contents along the Y axis.  
  600.      */
  601.     public void setVerticalAlignment(int alignment) {
  602.         if (alignment == verticalAlignment) return;
  603.         int oldValue = verticalAlignment;
  604.         verticalAlignment = checkVerticalKey(alignment, "verticalAlignment");
  605.         firePropertyChange("verticalAlignment", oldValue, verticalAlignment); 
  606.         repaint();
  607.     }
  608.  
  609.  
  610.     /**
  611.      * Returns the alignment of the label's contents along the X axis.
  612.      *
  613.      * @return   The value of the horizontalAlignment property, one of the 
  614.      *           following constants defined in <code>SwingConstants</code>:
  615.      *           <code>LEFT</code>,
  616.      *           <code>CENTER</code>, or
  617.      *           <code>RIGHT</code>.
  618.      *
  619.      * @see #setHorizontalAlignment
  620.      * @see SwingConstants
  621.      */
  622.     public int getHorizontalAlignment() {
  623.         return horizontalAlignment;
  624.     }
  625.  
  626.     /**
  627.      * Sets the alignment of the label's contents along the X axis.
  628.      * <p>
  629.      * This is a JavaBeans bound property.
  630.      *
  631.      * @param alignment  One of the following constants
  632.      *           defined in <code>SwingConstants</code>:
  633.      *           <code>LEFT</code> (the default for text-only labels),
  634.      *           <code>CENTER</code> (the default for image-only labels),
  635.      *           <code>RIGHT</code>,
  636.      *           <code>LEADING</code> or
  637.      *           <code>TRAILING</code>.
  638.      *
  639.      * @see SwingConstants
  640.      * @see #getHorizontalAlignment
  641.      * @beaninfo
  642.      *        bound: true
  643.      *         enum: LEFT     SwingConstants.LEFT
  644.      *               CENTER   SwingConstants.CENTER
  645.      *               RIGHT    SwingConstants.RIGHT
  646.      *               LEADING  SwingConstants.LEADING
  647.      *               TRAILING SwingConstants.TRAILING
  648.      *    attribute: visualUpdate true
  649.      *  description: The alignment of the label's content along the X axis.
  650.      */
  651.     public void setHorizontalAlignment(int alignment) {
  652.         if (alignment == horizontalAlignment) return;
  653.         int oldValue = horizontalAlignment;
  654.         horizontalAlignment = checkHorizontalKey(alignment,
  655.                                                  "horizontalAlignment");
  656.         firePropertyChange("horizontalAlignment",
  657.                            oldValue, horizontalAlignment);
  658.         repaint();
  659.     }
  660.  
  661.  
  662.     /**
  663.      * Returns the vertical position of the label's text,
  664.      * relative to its image.
  665.      *
  666.      * @return   One of the following constants
  667.      *           defined in <code>SwingConstants</code>:
  668.      *           <code>TOP</code>,
  669.      *           <code>CENTER</code>, or
  670.      *           <code>BOTTOM</code>.
  671.      *
  672.      * @see #setVerticalTextPosition
  673.      * @see SwingConstants
  674.      */
  675.     public int getVerticalTextPosition() {
  676.         return verticalTextPosition;
  677.     }
  678.  
  679.  
  680.     /**
  681.      * Sets the vertical position of the label's text,
  682.      * relative to its image.
  683.      * <p>
  684.      * The default value of this property is CENTER.
  685.      * <p>
  686.      * This is a JavaBeans bound property.
  687.      *
  688.      * @param textPosition  One of the following constants
  689.      *           defined in <code>SwingConstants</code>:
  690.      *           <code>TOP</code>,
  691.      *           <code>CENTER</code> (the default), or
  692.      *           <code>BOTTOM</code>.
  693.      *
  694.      * @see SwingConstants
  695.      * @see #getVerticalTextPosition
  696.      * @beaninfo
  697.      *        bound: true
  698.      *         enum: TOP    SwingConstants.TOP
  699.      *               CENTER SwingConstants.CENTER
  700.      *               BOTTOM SwingConstants.BOTTOM
  701.      *       expert: true
  702.      *    attribute: visualUpdate true
  703.      *  description: The vertical position of the text relative to it's image.
  704.      */
  705.     public void setVerticalTextPosition(int textPosition) {
  706.         if (textPosition == verticalTextPosition) return;
  707.         int old = verticalTextPosition;
  708.         verticalTextPosition = checkVerticalKey(textPosition,
  709.                                                 "verticalTextPosition");
  710.         firePropertyChange("verticalTextPosition", old, verticalTextPosition);
  711.         repaint();
  712.     }
  713.  
  714.  
  715.     /**
  716.      * Returns the horizontal position of the label's text,
  717.      * relative to its image.
  718.      *
  719.      * @return   One of the following constants
  720.      *           defined in <code>SwingConstants</code>:
  721.      *           <code>LEFT</code>,
  722.      *           <code>CENTER</code>, or
  723.      *           <code>RIGHT</code>.
  724.      *
  725.      * @see SwingConstants
  726.      */
  727.     public int getHorizontalTextPosition() {
  728.         return horizontalTextPosition;
  729.     }
  730.  
  731.  
  732.     /**
  733.      * Sets the horizontal position of the label's text,
  734.      * relative to its image.
  735.      *
  736.      * @param x  One of the following constants
  737.      *           defined in <code>SwingConstants</code>:
  738.      *           <code>LEFT</code>,
  739.      *           <code>CENTER</code>,
  740.      *           <code>RIGHT</code> (the default),
  741.      *           <code>LEADING</code>, or
  742.      *           <code>TRAILING</code>.
  743.      * @exception IllegalArgumentException
  744.      *
  745.      * @see SwingConstants
  746.      * @beaninfo
  747.      *       expert: true
  748.      *        bound: true
  749.      *         enum: LEFT     SwingConstants.LEFT
  750.      *               CENTER   SwingConstants.CENTER
  751.      *               RIGHT    SwingConstants.RIGHT
  752.      *               LEADING  SwingConstants.LEADING
  753.      *               TRAILING SwingConstants.TRAILING
  754.      *    attribute: visualUpdate true
  755.      *  description: The horizontal position of the label's text, 
  756.      *               relative to its image.
  757.      */
  758.     public void setHorizontalTextPosition(int textPosition) {
  759.         int old = horizontalTextPosition;
  760.         this.horizontalTextPosition = checkHorizontalKey(textPosition,
  761.                                                 "horizontalTextPosition");
  762.         firePropertyChange("horizontalTextPosition",
  763.                            old, horizontalTextPosition);
  764.         repaint();
  765.     }
  766.  
  767.  
  768.     /** 
  769.      * See readObject() and writeObject() in JComponent for more 
  770.      * information about serialization in Swing.
  771.      */
  772.     private void writeObject(ObjectOutputStream s) throws IOException {
  773.         s.defaultWriteObject();
  774.         if ((ui != null) && (getUIClassID().equals(uiClassID))) {
  775.             ui.installUI(this);
  776.         }
  777.     }
  778.  
  779.  
  780.     /**
  781.      * Returns a string representation of this JLabel. This method 
  782.      * is intended to be used only for debugging purposes, and the 
  783.      * content and format of the returned string may vary between      
  784.      * implementations. The returned string may be empty but may not 
  785.      * be <code>null</code>.
  786.      * <P>
  787.      * Overriding paramString() to provide information about the
  788.      * specific new aspects of the JFC components.
  789.      * 
  790.      * @return  a string representation of this JLabel.
  791.      */
  792.     protected String paramString() {
  793.     String textString = (text != null ?
  794.                  text : "");
  795.     String defaultIconString = (defaultIcon != null ?
  796.                     defaultIcon.toString() : "");
  797.     String disabledIconString = (disabledIcon != null ?
  798.                      disabledIcon.toString() : "");
  799.     String labelForString = (labelFor  != null ?
  800.                  labelFor.toString() : "");
  801.         String verticalAlignmentString;
  802.         if (verticalAlignment == TOP) {
  803.             verticalAlignmentString = "TOP";
  804.         } else if (verticalAlignment == CENTER) {
  805.             verticalAlignmentString = "CENTER";
  806.         } else if (verticalAlignment == BOTTOM) {
  807.             verticalAlignmentString = "BOTTOM";
  808.         } else verticalAlignmentString = "";
  809.         String horizontalAlignmentString;
  810.         if (horizontalAlignment == LEFT) {
  811.             horizontalAlignmentString = "LEFT";
  812.         } else if (horizontalAlignment == CENTER) {
  813.             horizontalAlignmentString = "CENTER";
  814.         } else if (horizontalAlignment == RIGHT) {
  815.             horizontalAlignmentString = "RIGHT";
  816.         } else horizontalAlignmentString = "";
  817.         String verticalTextPositionString;
  818.         if (verticalTextPosition == TOP) {
  819.             verticalTextPositionString = "TOP";
  820.         } else if (verticalTextPosition == CENTER) {
  821.             verticalTextPositionString = "CENTER";
  822.         } else if (verticalTextPosition == BOTTOM) {
  823.             verticalTextPositionString = "BOTTOM";
  824.         } else verticalTextPositionString = "";
  825.         String horizontalTextPositionString;
  826.         if (horizontalTextPosition == LEFT) {
  827.             horizontalTextPositionString = "LEFT";
  828.         } else if (horizontalTextPosition == CENTER) {
  829.             horizontalTextPositionString = "CENTER";
  830.         } else if (horizontalTextPosition == RIGHT) {
  831.             horizontalTextPositionString = "RIGHT";
  832.         } else horizontalTextPositionString = "";
  833.  
  834.     return super.paramString() +
  835.     ",defaultIcon=" + defaultIconString +
  836.     ",disabledIcon=" + disabledIconString +
  837.     ",horizontalAlignment=" + horizontalAlignmentString +
  838.     ",horizontalTextPosition=" + horizontalTextPositionString +
  839.     ",iconTextGap=" + iconTextGap +
  840.     ",labelFor=" + labelForString +
  841.     ",text=" + textString +
  842.     ",verticalAlignment=" + verticalAlignmentString +
  843.     ",verticalTextPosition=" + verticalTextPositionString;
  844.     }
  845.  
  846.     /**
  847.      * --- Accessibility Support ---
  848.      */
  849.  
  850.     /** 
  851.      * Get the AccessibleContext of this object 
  852.      *
  853.      * @return the AccessibleContext of this object
  854.      * @beaninfo
  855.      *       expert: true
  856.      *  description: The AccessibleContext associated with this Label.
  857.      */
  858.     public AccessibleContext getAccessibleContext() {
  859.         if (accessibleContext == null) {
  860.             accessibleContext = new AccessibleJLabel();
  861.         }
  862.         return accessibleContext;
  863.     }
  864.  
  865.     /**
  866.      * The class used to obtain the accessible role for this object.
  867.      * <p>
  868.      * <strong>Warning:</strong>
  869.      * Serialized objects of this class will not be compatible with
  870.      * future Swing releases.  The current serialization support is appropriate
  871.      * for short term storage or RMI between applications running the same
  872.      * version of Swing.  A future release of Swing will provide support for
  873.      * long term persistence.
  874.      */
  875.     protected class AccessibleJLabel extends AccessibleJComponent {
  876.  
  877.         /**
  878.          * Get the accessible name of this object.  
  879.          * 
  880.          * @return the localized name of the object -- can be null if this 
  881.          * object does not have a name
  882.          * @see AccessibleContext#setAccessibleName
  883.          */
  884.         public String getAccessibleName() {
  885.             if (accessibleName != null) {
  886.                 return accessibleName;
  887.             } else {
  888.                 if (getText() == null) {
  889.                     return super.getAccessibleName();
  890.                 } else {
  891.                     return getText();
  892.                 }
  893.             }
  894.         }
  895.  
  896.         /**
  897.          * Get the role of this object.
  898.          *
  899.          * @return an instance of AccessibleRole describing the role of the 
  900.          * object
  901.          * @see AccessibleRole
  902.          */
  903.         public AccessibleRole getAccessibleRole() {
  904.             return AccessibleRole.LABEL;
  905.         }
  906.  
  907.     }  // AccessibleJComponent
  908.  
  909.     /**
  910.      * Get the component this is labelling.
  911.      *
  912.      * @return the Component this is labelling.  Can be null if this
  913.      * does not label a Component.  If the displayedMnemonic 
  914.      * property is set and the labelFor property is also set, the label 
  915.      * will call the requestFocus method of the component specified by the
  916.      * labelFor property when the mnemonic is activated.
  917.      *
  918.      * @see #getDisplayedMnemonic
  919.      * @see #setDisplayedMnemonic
  920.      */
  921.     public Component getLabelFor() {
  922.         return labelFor;
  923.     }
  924.  
  925.     /**
  926.      * Set the component this is labelling.  Can be null if this does not 
  927.      * label a Component.  If the displayedMnemonic property is set
  928.      * and the labelFor property is also set, the label will
  929.      * call the requestFocus method of the component specified by the
  930.      * labelFor property when the mnemonic is activated.
  931.      *
  932.      * @param c  the Component this label is for, or null if the label is
  933.      *           not the label for a component
  934.      *
  935.      * @see #getDisplayedMnemonic
  936.      * @see #setDisplayedMnemonic
  937.      * @beaninfo
  938.      *        bound: true
  939.      *  description: The component this is labelling.
  940.      */
  941.     public void setLabelFor(Component c) {
  942.         Component oldC = labelFor;
  943.         labelFor = c;
  944.         firePropertyChange("labelFor", oldC, c);        
  945.  
  946.         if (oldC instanceof JComponent) {
  947.             ((JComponent) c).putClientProperty(LABELED_BY_PROPERTY,null);
  948.         }
  949.         if (c instanceof JComponent) {
  950.             ((JComponent) c).putClientProperty(LABELED_BY_PROPERTY,this);
  951.         }
  952.     }
  953.  
  954. }
  955.